using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;
using Net.SourceForge.WURFL.Core;

namespace Roaming.view
{
    public partial class outinfo : System.Web.UI.Page
    {
        protected int iCateId = 0;
        protected int iCityId = 0;
        protected int cols = 3;
        protected DataSet ds = null;
        protected DataSet dsMenu = null;
        protected DataSet dsArticle = null;
        private int PageIndex = 0;
        protected int curPage = 0;
        protected string strPageLink = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUA = Request.Headers["USER-AGENT"].ToString();
            if (strUA.ToLower().Contains("opera"))
            {
                if (Request.Headers["X-OperaMini-Phone-UA"] != null)
                    strUA = Request.Headers["X-OperaMini-Phone-UA"].ToString();
            }
            cols = DAL.getColLinkSite(strUA);
            if (cols > 2)
            {
                cols = 3;
            }
            if ((Request.QueryString["catId"] != null) && (Request.QueryString["catId"].ToString() != ""))
                iCateId = Int32.Parse(Request.QueryString["catId"].ToString());
            //if ((Request.QueryString["cityID"] != null) && (Request.QueryString["cityID"].ToString() != ""))
            //    iCity = Int32.Parse(Request.QueryString["cityID"].ToString());
            try
            {
                iCityId = Int32.Parse(Session["CityId"].ToString());
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            curPage = PageIndex = ((Request.QueryString["page"]) == null) ? 0 : Convert.ToInt32(Request.QueryString["page"].ToString());
            if (!Page.IsPostBack)
            {
                    if (iCateId == 91)
                      Response.Redirect("http://facebook.com");
                    BindDataById(iCateId);               
            }
        }
      
        protected void BindDataById(int iCateId)
        {
     
            string strSQL3 = "";
            if (iCateId == 340)
                strSQL3 = string.Format("exec tblTouristContent_getByCondition1 N'and touristguideId=3 and CityID ={0} and status=1'", iCityId);
            else
                strSQL3 = string.Format("exec tblArticle_GetTopNByCateId {0},{1}", 0, iCateId);

                DataSet dsCheck = DAL.GetDataSet(strSQL3);
                if (dsCheck.Tables[0].Rows.Count > 0) //Category
                {
                    if (PageIndex == 0) PageIndex = 1;
                    int count = 0;
                    ArrayList arr = null;
                    try
                    {
                        arr = DAL.GetDataReaderToArrayList(strSQL3);
                        count = arr.Count;
                        int iRecordPerPage = 3;
                        int PageCount = (int)count % iRecordPerPage;
                        if (PageCount == 0)
                            PageCount = (int)count / iRecordPerPage;
                        else
                            PageCount = (int)count / iRecordPerPage + 1;
                        if (PageIndex > PageCount) PageIndex = PageCount;
                        if ((PageCount == 0) || (PageCount == 1))
                        {
                            strPageLink = "<a href=\"/" + "view/index.html" + ".htm\">" + "Back" + "</a> | ";
                            PageIndex = 1;
                        }
                        else
                        {
                            strPageLink = WritePageLink(PageIndex, PageCount);
                        }
                        ds = DAL.ListImageData(strSQL3, PageIndex, iRecordPerPage, "ItemImageMms");
                        rptList.DataSource = ds;
                        rptList.DataBind();
                        rptList.Dispose();
                    }
                    catch (Exception e)
                    {
                        e.ToString();
                        ds.Dispose();
                    }
                }

        }
      
        private string WritePageLink(int CurrentPage, int PageCount)
        {

            string sHTML = "";

            if (PageCount <= 6)
            {

                for (int i = 1; i <= PageCount; i++)
                {
                    if (i > 1)
                        sHTML += " | ";
                    sHTML += BuildURL(Convert.ToSingle(i) + "  ", i);
                }
                return sHTML;
            }
            else
            {
                if (CurrentPage <= 6)
                {
                    for (int i = 1; i <= 6; i++)
                    {
                        if (i > 1)
                            sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(i) + "  ", i);
                    }
                    sHTML += BuildURL("Next |", CurrentPage + 1);
                    return sHTML;
                }
                else
                {
                    if (CurrentPage == PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 2) + "  ", CurrentPage - 2);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += BuildURL(" Back |", CurrentPage - 1);
                        return sHTML;
                    }
                    if (CurrentPage + 1 == PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage + 1) + "  ", CurrentPage + 1);
                        sHTML += BuildURL(" Back |", CurrentPage - 2);
                        return sHTML;
                    }
                    if (CurrentPage + 1 < PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage + 1) + "  ", CurrentPage + 1);
                        sHTML += BuildURL(" Next |", CurrentPage + 1);
                        return sHTML;
                    }

                }
            }
            return sHTML;
        }
        private string BuildURL(string text, int PageIndex)
        {
            string result = "";
            if (curPage != PageIndex)
            {
                //result = "<a href=\"info.aspx?catId=" + iCateId + "&page=" + PageIndex + "\">" + text + "</a>";
                result = "<a href=\"/" + iCateId + "/outinfo-page" + PageIndex + ".html\">" + text + "</a>";
            }
            else
                result = text;
            return result;
        }
    }
}
